function Range = specrange(This,Range,Flag)
% specrange  Time series specific range.
%
%
% Syntax
% =======
%
%     Rng = specrange(X,S)
%
%
% Input arguments
% ================
%
% * `X` [ tseries ] - Time series.
%
% * `S` [ numeric | `@all` ] - Range specification; the output range `Rng`
% will be constructed from the first and the last element of `S` only.
%
%
% Output arguments
% =================
%
% * `Rng` [ numeric ] - Date range constructed from `S` specific to time
% series `X`.
%
%
% Description
% ============
%
% The time series specific range is constructed as `startDate:endDate`
% where
%
% * the start date `startDate` is `S(1)` if `S(1)` is a serial date number,
% or the start date of the input series `X` if `S(1)` is `Inf`, `-Inf`, or
% `@all`;
%
% * the end date `endDate` is `S(end)` if `S(end)` is a serial date number,
% or the end date of the input series `X` if `S(end)` is `Inf`, or `@all`.
%
%
% Example
% ========
%
% Create a time series from `2000Q1` to `2001Q4` 
%
%     >> x = tseries( qq(2000,1):qq(2001,4), @rand );
%
% The function `specrange` returns the full range of the time series when
% `S` is `Inf`
%
%     >> dat2str( specrange(x,Inf) )
%     ans = 
%       Columns 1 through 6
%         '2000Q1'    '2000Q2'    '2000Q3'    '2000Q4'    '2001Q1'    '2001Q2'
%       Columns 7 through 8
%         '2001Q3'    '2001Q4'
%
% or when `S` is `[-Inf,Inf]`
%
%     >> dat2str( specrange(x,[-Inf,Inf]) )
%     ans = 
%       Columns 1 through 6
%         '2000Q1'    '2000Q2'    '2000Q3'    '2000Q4'    '2001Q1'    '2001Q2'
%       Columns 7 through 8
%         '2001Q3'    '2001Q4'
%
% or when `S` is `@all`
%
%     >> dat2str( specrange(x,@all) )
%     ans = 
%       Columns 1 through 6
%         '2000Q1'    '2000Q2'    '2000Q3'    '2000Q4'    '2001Q1'    '2001Q2'
%       Columns 7 through 8
%         '2001Q3'    '2001Q4'
%
% A range from the start of the time series to a specific date is returned
% when `S(1)` is `-Inf` and `S(end)` is that specific end date:
%
%     >> dat2str( specrange(x,[-Inf,qq(2000,3)]) )
%     ans = 
%         '2000Q1'    '2000Q2'    '2000Q3'
%
% A range from a specific date to the end of the time series is returned
% when `S(1)` is that specific start date date, and `S(end)` is `Inf`:
%
%     >> dat2str( specrange(x,[qq(2000,3),Inf]) )
%     ans = 
%         '2000Q3'    '2000Q4'    '2001Q1'    '2001Q2'    '2001Q3'    '2001Q4'
%

% -IRIS Toolbox.
% -Copyright (c) 2007-2015 IRIS Solutions Team.

try
    Flag; %#ok<VUNUS>
catch
    Flag = 'max';
end

%--------------------------------------------------------------------------

len = size(This.data,1);

if isequal(Range,Inf) || isequal(Range,@all) || isequal(Range,':')
    Range = This.start + (0 : len-1);
    if strcmpi(Flag,'min')
        ixObs = all(~isnan(This.data(:,:)),2);
        if any(ixObs)
            first = find(ixObs,1,'first');
            last = find(ixObs,1,'last');
            Range = Range(first:last);
        else
            Range = zeros(1,0);
        end
    end
    return
end

if isempty(Range) || all(isnan(Range))
    Range = zeros(1,0);
    return
end

if isinf(Range(1))
    startDate = This.start;
else
    startDate = Range(1);
end

if isinf(Range(end))
    endDate = This.start + len - 1;
else
    endDate = Range(end);
end

Range = startDate : endDate;

end
